package com.yulinlin.admin.modules.im.service.impl;

import com.yulinlin.admin.modules.im.domain.ImSessionEntity;
import com.yulinlin.admin.modules.im.domain.ImSessionUserEntity;
import com.yulinlin.admin.modules.im.domain.enums.ImSessionEnums;
import com.yulinlin.admin.modules.im.domain.enums.ImSessionUserEnums;
import com.yulinlin.admin.modules.im.service.IImSessionService;
import com.yulinlin.admin.modules.im.dao.ImSessionDao;

import com.yulinlin.common.model.ModelSelectWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yulinlin.common.service.impl.ServiceImpl;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;

@Service

public class ImSessionServiceImpl extends ServiceImpl<ImSessionEntity> implements IImSessionService  {


    @Autowired
    private ImSessionDao dao;

    @Override
    public List<ImSessionEntity> mySessionList(String userId) {
       List<ImSessionUserEntity> userList =  ModelSelectWrapper.newInstance(ImSessionUserEntity.class)
        .eq("sysUserId",userId)
        .selectList();



       Collection ids = new HashSet<>();
        for (ImSessionUserEntity imSessionUserEntity : userList) {
            ids.add(imSessionUserEntity.getImSessionId());

        }
       return ModelSelectWrapper.newInstance(ImSessionEntity.class)
                .in("id",ids)
                .selectList();


    }

    @Override
    public ImSessionEntity group(String userId) {
        /**
         * 创建一个会话
         * 添加会话用户，标记群主
         */
        //会话房间
        ImSessionEntity sessionEntity = new ImSessionEntity();
        sessionEntity.setSessionType(ImSessionEnums.sessionType.one_to_many);
        sessionEntity.setTitle("群聊");
        sessionEntity.createInsertWrapper().execute();

        //会话用户
        ImSessionUserEntity entity = new ImSessionUserEntity();
        entity.setUserId(userId);
        entity.setImSessionId(sessionEntity.getId());
        entity.setRole(ImSessionUserEnums.role.master);
        entity.createInsertWrapper().execute();
        return sessionEntity;
    }

    @Override
    public ImSessionEntity oneToOne(String fromUser, String toUser) {
        //判断会话是否存在
        //
        ImSessionEntity sessionEntity
         =ModelSelectWrapper.newInstance(ImSessionEntity.class)
                .eq("sessionType",ImSessionEnums.sessionType.one_to_one)
                .eq("fromUser",fromUser)
                .eq("toUser",toUser)
                .selectOne();

        if(sessionEntity != null){
            return sessionEntity;
        }

        //添加房间
        sessionEntity = new ImSessionEntity();
        sessionEntity.setSessionType(ImSessionEnums.sessionType.one_to_one);
        sessionEntity.setFromUser(fromUser);
        sessionEntity.setToUser(toUser);
        sessionEntity.setTitle("私聊");
        sessionEntity.createInsertWrapper().execute();

        //添加会话用户
        ImSessionUserEntity from =   new ImSessionUserEntity();
        from.setImSessionId(sessionEntity.getId());
        from.setUserId(fromUser);
        from.createInsertWrapper().execute();

        //拉用户进来
        ImSessionUserEntity entity = new ImSessionUserEntity();
        entity.setUserId(toUser);
        entity.setImSessionId(sessionEntity.getId());
        entity.createInsertWrapper().execute();

        return sessionEntity;
    }
}
